using System;
using System.Collections.Generic;
using System.Text;
using Stormwind.Testing.Entities;
using Stormwind.Core.Data;
using Castle.ActiveRecord;

namespace Stormwind.Testing {
	/// <summary>
	/// Creates sample objects.
	/// </summary>
	public static class ObjectMother {
		/// <summary>
		/// Initializes the database with sample data.
		/// </summary>
		/// <param name="numberOfCustomers">Number of customers.</param>
		public static void InitializeDatabase(int numberOfCustomers, int numberOfOrders) {
			for (int i = 0; i < numberOfCustomers; i++) {
				CreateSampleCustomer(i, numberOfOrders);
			}
		}

		/// <summary>
		/// Creates a sample customer with a (50 + number of customer) orders.
		/// </summary>
		/// <returns>Returns the created customer.</returns>
		public static Customer CreateSampleCustomer(int customerNumber, int numberOfOrders) {
			Customer cust = new Customer();
			Random rnd = new Random();
			cust.Name = "Test Customer Inc. " + customerNumber.ToString();

			for (int i = 0; i < numberOfOrders; i++) {
				OrderItem ord1 = new OrderItem();
				ord1.Customer = cust;
				ord1.Date = DateTime.Now.AddDays(-1 * i);
				ord1.Value = Math.Round(rnd.NextDouble() * 10000, 2);
				cust.Orders.Add(ord1);
			}

			ActiveRecordMediator<Customer>.Create(cust);

			return cust;
		}

		/// <summary>
		/// Creates sample data definition for a report.
		/// </summary>
		/// <returns>Returns the filled object.</returns>
		public static DataDefinition CreateSampleDataDefinition() {
			DataDefinition def = new DataDefinition();

			//Data Sources.
			def.DataSources.Add(new DataSource());
			def.DataSources.Add(new DataSource());

			def.DataSources[0].AssemblyName = typeof(Customer).Assembly.FullName;
			def.DataSources[0].ClassNamespace = typeof(Customer).Namespace;
			def.DataSources[0].ClassName = typeof(Customer).Name;

			def.DataSources[1].AssemblyName = typeof(OrderItem).Assembly.FullName;
			def.DataSources[1].ClassNamespace = typeof(OrderItem).Namespace;
			def.DataSources[1].ClassName = typeof(OrderItem).Name;

			//Fields.
			def.Fields.Add(new FieldDefinition());
			def.Fields.Add(new FieldDefinition());
			def.Fields.Add(new FieldDefinition());
			def.Fields.Add(new FieldDefinition());
			def.Fields.Add(new FieldDefinition());

			def.Fields[0].DataSource = def.DataSources[0];
			def.Fields[0].IsFilterable = true;
			def.Fields[0].PropertyName = "Id";
			def.Fields[0].PropertyType = DataTypes.Integer;

			def.Fields[1].DataSource = def.DataSources[0];
			def.Fields[1].IsFilterable = true;
			def.Fields[1].PropertyName = "Name";
			def.Fields[1].PropertyType = DataTypes.String;

			def.Fields[2].DataSource = def.DataSources[1];
			def.Fields[2].IsFilterable = true;
			def.Fields[2].PropertyName = "Id";
			def.Fields[2].PropertyType = DataTypes.Integer;

			def.Fields[3].DataSource = def.DataSources[1];
			def.Fields[3].IsFilterable = true;
			def.Fields[3].PropertyName = "Date";
			def.Fields[3].PropertyType = DataTypes.Date;

			def.Fields[4].DataSource = def.DataSources[1];
			def.Fields[4].IsFilterable = true;
			def.Fields[4].PropertyName = "Value";
			def.Fields[4].PropertyType = DataTypes.Double;

			//Relations.
			def.Relations.Add(new DataRelation());
			def.Relations[0].Origin = def.DataSources[0];
			def.Relations[0].Destiny = def.DataSources[1];
			def.Relations[0].DestinyProperty = "Customer";

			//Groupings.
			def.Groupings.Add(new GroupingDefinition());
			def.Groupings.Add(new GroupingDefinition());
			def.Groupings[0].Field = def.Fields[0];
			def.Groupings[0].Sequence = 1;
			def.Groupings[1].Field = def.Fields[2];
			def.Groupings[1].Sequence = 2;

			return def;
		}
	}
}
